package com.dly.blog.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dly.blog.content.RedisContent;
import com.dly.blog.domain.Comment;
import com.dly.blog.domain.vo.comment.CommentPageVO;
import com.dly.blog.utile.RedisUtils;
import com.dly.blog.utile.SecurityUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dly.blog.controller.BaseController;
import lombok.AllArgsConstructor;
import com.dly.blog.domain.vo.comment.CommentVO;
import com.dly.blog.domain.ro.comment.CommentPageRO;
import com.dly.blog.domain.ro.comment.CommentAddRO;
import com.dly.blog.domain.ro.comment.CommentEditRO;
import com.dly.blog.service.CommentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.dly.blog.domain.vo.PageVO;
import com.dly.blog.msg.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author DLY
 * @since 2024-01-30 13:17:33
 */
@Tag(name = "评论相关")
@AllArgsConstructor
@RestController
@RequestMapping("/content/comment")
public class CommentController extends BaseController {

    private final CommentService commentService;

    private final RedisUtils redisUtils;

    @PostMapping("/support/{id}/{isSupport}")
    @Operation(summary = "给评论点赞")
    public ResponseEntity<R<Void>> support(@PathVariable(value = "id") String id, @PathVariable(value = "isSupport") boolean isSupport){
        if(isSupport) {
            redisUtils.removeSet(RedisContent.COMMENT_SUPPORT + id, SecurityUtils.getUserId());
            commentService.update(new LambdaUpdateWrapper<Comment>()
                    .eq(Comment::getId, id)
                    .set(Comment::getKudosNum, redisUtils.getSet(RedisContent.COMMENT_SUPPORT + id).size())
            );
        }else {
            redisUtils.addSet(RedisContent.COMMENT_SUPPORT + id, SecurityUtils.getUserId());
            commentService.update(new LambdaUpdateWrapper<Comment>()
                    .eq(Comment::getId, id)
                    .set(Comment::getKudosNum, redisUtils.getSet(RedisContent.COMMENT_SUPPORT + id).size())
            );
        }
        return operate(this::success);
    }

    @PostMapping(value = "/page")
    @Operation(summary = "分页获取评论信息")
    public ResponseEntity<R<PageVO<CommentPageVO>>> getCommentPage(@RequestBody CommentPageRO commentPageRO){
        return operate(()-> this.successDate(commentService.getCommentPage(commentPageRO)));
    }

    @PostMapping(value = "/sonPage")
    @Operation(summary = "分页获取子评论信息")
    public ResponseEntity<R<PageVO<CommentPageVO>>> getCommentSonPage(@RequestBody CommentPageRO commentPageRO, BindingResult br){
        return operate(()-> this.successDate(commentService.getCommentSonPage(commentPageRO)), br);
    }

    @PostMapping(value = "/add")
    @Operation(summary = "添加评论信息")
    public ResponseEntity<R<Void>> addComment(@RequestBody CommentAddRO commentAddRO){
        commentService.addComment(commentAddRO);
        return operate(this::success);
    }

    @PostMapping(value = "/edit")
    @Operation(summary = "修改评论信息")
    public ResponseEntity<R<Void>> editComment(@RequestBody CommentEditRO commentEditRO){
        commentService.editComment(commentEditRO);
        return operate(this::success);
    }

    @PostMapping(value = "/get/{id}")
    @Operation(summary = "获取评论信息")
    public ResponseEntity<R<CommentVO>> getComment(@PathVariable(value = "id") String id){
        return operate(()-> this.successDate(commentService.getComment(id)));
    }

    @PostMapping(value = "/remove/{ids}")
    @Operation(summary = "删除评论信息")
    public ResponseEntity<R<Void>> removeComment(@PathVariable(value = "ids") String[] ids){
        commentService.removeComment(ids);
        return operate(this::success);
    }

}
